﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Readify.Management.Automation.GoGrid
{
    public class GoGridLoadBalancer
    {
        internal static GoGridLoadBalancer CreateFromXmlNode(XmlNode objectElementNode)
        {
            GoGridLoadBalancer output = new GoGridLoadBalancer();
            output.ID = GoGridHelper.GetAttributeElementValueFromXmlNodeAsNullableInt32(objectElementNode, "id");
            output.Name = GoGridHelper.GetAttributeElementValueFromXmlNodeAsString(objectElementNode, "name");
            output.Description = GoGridHelper.GetAttributeElementValueFromXmlNodeAsString(objectElementNode, "description");

            XmlNode virtualInterfaceAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "virtualip");
            output.VirtualInterface = GoGridLoadBalancerInterface.CreateFromXmlNode(virtualInterfaceAttributeXmlNode);

            XmlNode realInterfacesAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsXmlNode(objectElementNode, "realiplist");
            XmlNodeList realInterfacesXmlNodeList = realInterfacesAttributeXmlNode.SelectNodes("list/object[@name = 'ipportpair']");

            List<GoGridLoadBalancerInterface> temporaryRealInterfaceList = new List<GoGridLoadBalancerInterface>();
            foreach (XmlNode realInterfacesXmlNode in realInterfacesXmlNodeList)
            {
                GoGridLoadBalancerInterface realInterface = GoGridLoadBalancerInterface.CreateFromXmlNode(realInterfacesXmlNode);
                temporaryRealInterfaceList.Add(realInterface);
            }
            output.RealInterfaces = temporaryRealInterfaceList.ToArray();

            XmlNode typeAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "type");
            output.Type = GoGridOption.CreateFromXmlNode(typeAttributeXmlNode);

            XmlNode persistenceAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "persistence");
            output.Persistence = GoGridOption.CreateFromXmlNode(persistenceAttributeXmlNode);

            XmlNode operatingSystemAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "os");
            output.OperatingSystem = GoGridOption.CreateFromXmlNode(operatingSystemAttributeXmlNode);

            XmlNode stateAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "state");
            output.State = GoGridOption.CreateFromXmlNode(stateAttributeXmlNode);

            return output;
        }

        public int? ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public GoGridLoadBalancerInterface VirtualInterface { get; set; }
        public GoGridLoadBalancerInterface[] RealInterfaces { get; set; }
        public GoGridOption Type { get; set; }
        public GoGridOption Persistence { get; set; }
        public GoGridOption OperatingSystem { get; set; }
        public GoGridOption State { get; set; }
    }
}
